from django import forms # 参考P145
from django.contrib.auth.models import User
from django.forms import Form

from post.models import Topic


# 20-Form表单进阶-新增话题
class TopicModelForm(forms.ModelForm):

    # title = forms.CharField()
    # x = forms.CharField()

    class Meta:
        model = Topic
        # fields = '__all__'
        exclude = ['is_online']

    # 对话题正文内容进行违禁词汇验证
    # def clean_content(self):
    #     content = self.cleaned_data['content']
    #
    #     ban_list = ["秒杀", "抢爆", "疯抢", "抢疯了"]
    #
    #     cut_content = jieba.lcut(content)
    #     check = list(set(cut_content) & set(ban_list))
    #
    #     if check:
    #         self.add_error('content', '正文内容包含违禁词汇')
    #     return content

class LoginForm(Form):
    username = forms.CharField(label='用户名', min_length=5)
    pwd      = forms.CharField(label='密码', min_length=5, widget=forms.PasswordInput)

    def clean_username(self):
        user_name = self.cleaned_data['username']

        users = User.objects.filter(username=user_name)

        if not users.exists():
            self.add_error('username', '用户名不存在')

        return user_name